<template>

  <div class="container">
    <el-form inline label-width="auto">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="query.name" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" :icon="Search" @click="handleQuery">检索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="showAddDept" :icon="Plus">添加部门</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" border striped>
      <el-table-column label="序号" type="index" align="center" width="80" />
      <el-table-column label="部门名称" prop="name" />
      <el-table-column label="添加时间" prop="createTime" align="center" width="160" />
      <el-table-column label="修改时间" prop="updateTime" align="center" width="160" />
      <el-table-column label="操作" prop="op">
        <template v-slot="{ row }">
          <el-button type="warning" :icon="Edit" @click="updaetRow(row)">编辑</el-button>
          <el-button type="danger" :icon="Delete" @click="deleteRow(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 10px;" layout="prev, pager, next" :total="total" :page-size="query.pageSize"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    <el-dialog v-model="dialogVisible" :title="title" width="500">
      <el-form ref="ruleFormRef" :model="form" label-width="auto" :rules="rules" style="max-width: 600px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitData">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { Delete, Edit, Plus, Search } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

import { listDept, getDeptInfo, deleteDept, addDept, updateDept } from '@/api/dept.js';
import { onMounted, reactive, ref } from 'vue'
const list = ref([])
const dialogVisible = ref(false)
const form = ref({
  id: undefined,
  name: undefined,
})
const title = ref("")
const total = ref(0)
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  deptName: "",
})
const ruleFormRef = ref()

const rules = reactive({
  name: [
    { required: true, message: '部门名称不能为空', trigger: 'blur' }
  ],
})

onMounted(async () => {
  getList();
})

function handleSizeChange(size) {
  query.pageSize = size;
  getList();
}

function handleCurrentChange(current) {
  query.pageNum = current;
  getList();
}


async function getList() {
  let data = await listDept(query)
  list.value = data.rows;
  total.value = data.total;
  console.log(total.value);
}
function showAddDept() {
  title.value = "添加部门"
  form.value = {
    id: undefined,
    name: undefined,
  }
  dialogVisible.value = true

}
function handleQuery() {
  query.pageNum = 1
  getList();
}
function deleteRow(row) {
  ElMessageBox.confirm(
    `是否确认删除部门${row.name}?`,
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      deleteDept(row.id)
        .then(() => {
          ElMessage({
            message: '删除成功',
            type: 'success',
          })
          getList();
        })
    })
    .catch(() => {

    })
}

function updateRow(row) {
  title.value = "修改部门"
  getDeptInfo(row.id)
    .then(res => {
      form.value = {
        id: res.data.id,
        name: res.data.name
      }
      dialogVisible.value = true
    })
}

async function submitData() {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (form.value.id) {
        updateDept({
          id: form.value.id,
          name: form.value.name
        })
          .then(() => {
            ElMessage({
              message: '修改成功',
              type: 'success',
            })
            dialogVisible.value = false
            getList();
          })
      } else {
        addDept({
          name: form.value.name
        })
          .then(() => {
            ElMessage({
              message: '添加成功',
              type: 'success',
            })
            dialogVisible.value = false
            getList();
          })
      }
    } else {
      console.log('error submit!', fields)
    }
  })


}
</script>
